package service

import (
	"github.com/quarkcloudio/quark-go/v2/pkg/builder"
	"github.com/wsl/hi-go/model"
	customTool "github.com/wsl/hi-go/plugins/customTool"
	sqlManager "github.com/wsl/hi-go/plugins/sql"
	"github.com/wsl/hi-go/types"
)

type StudentService struct{}

// 创建学生
func (studentService *StudentService) CreateStudent(c *builder.Context) error {
	var student model.Student
	c.Bind(&student)
	// 检查参数是否合规，不合规提示默认提示语
	if _, err := customTool.CheckQueryV2(student); err != nil {
		return c.JSONError(err.Error())
	}
	if err := (&model.Student{}).Create(student.Name, student.Age, *student.ClassId); err != nil {
		return c.JSONError(err.Error())
	}
	return c.JSONOk("操作成功")
}

// 查询学生列表
func (studentService *StudentService) StudentList(c *builder.Context) error {
	page, pageSize := sqlManager.PageManagerGetQuery(c)
	result, err := (&model.Student{}).Index(page, pageSize)
	if err != nil {
		return c.JSONError(err.Error())
	} else {
		return c.JSONOk("操作成功", map[string]interface{}{
			"list":  result.List,
			"total": result.Total,
		})
	}
}

// 更改学生班级信息
func (studentService *StudentService) StudentChangeClassInfo(c *builder.Context) error {
	var updateStudentClassParam types.UpdateStudentClassParam
	c.Bind(&updateStudentClassParam)
	// 检查参数是否合规，不合规提示默认提示语
	if _, err := customTool.CheckQueryV2(updateStudentClassParam); err != nil {
		return c.JSONError(err.Error())
	}
	err := (&model.Student{}).BindStudentToClass(updateStudentClassParam)
	if err != nil {
		return c.JSONError(err.Error())
	} else {
		return c.JSONOk("操作成功")
	}
}
